"""
Case Type   : 服务端工具
Case Name   : 验证gs_collector工具功能
Create At   : 2024.10.8
Owner       : wang-tianjing1
Description :
    1.显示帮助信息
    2.显示版本号
    3。收集日志
Expect      :
    1.显示正确
    2.显示正确
    3.成功
History     :
"""
import unittest
import os
import time
from yat.test import Node
from yat.test import macro
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger



class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_node = Node('PrimaryDbUser')
        self.Constant = Constant()
        self.tmp_dir_path = os.path.join(os.path.dirname(macro.PG_LOG_PATH))

    def test_server_tools1(self):
        self.log.info('------------------显示帮助信息------------------')
        cmd_list = ['-?', '--help']
        for cmd in cmd_list:
            check_cmd1 = f'''source {macro.DB_ENV_PATH}
                 gs_collector {cmd}'''
            self.log.info(check_cmd1)
            msg1 = self.primary_node.sh(check_cmd1).result()
            self.log.info(msg1)

            start_index1 = msg1.find('Usage:')+len('Usage:')
            start_index2 = msg1.find('General options:')
            temp = msg1[start_index1:start_index2].split('\n')
            self.log.info(temp)
            options_list1 = []
            for j in temp[1:-2]:
                options_list1.append(j.strip())
            self.log.info(options_list1)
            grammar = [
                'gs_collector -? | --help',
                'gs_collector -V | --version',
                'gs_collector --begin-time="BEGINTIME" --end-time="ENDTIME" [-h HOSTNAME | -f HOSTFILE]',
                '[--keyword=KEYWORD] [--speed-limit=SPEED] [-o OUTPUT] [-l self.logFILE]']
            if len(options_list1) == len(grammar):
                for opt in options_list1:
                    if opt in grammar:
                        self.log.info(f'{opt}---------语法校验通过---------')
                    else:
                        self.log.error(f'{opt}---------语法校验不通过---------')
            else:
                self.log.error('---------语法校验有误---------')

            options_list2 = []
            for i in msg1[start_index2:].split('\n'):
                for j in i.split(' '):
                    if len(j) != 0:
                        if j[0] == '-':
                            options_list2.append(j)
                        else:
                            pass
                    else:
                        pass
            self.log.info(options_list2)
            parameter = [
                '--begin-time=BEGINTIME', '--end-time=ENDTIME', '--speed-limit=SPEED', '-h', '-f',
                '--keyword=KEYWORD', '-o', '-l', '-?,', '--help', '-V,', '--version', '-C']
            if len(options_list2) == len(parameter):
                for opt in options_list2:
                    if opt in parameter:
                        self.log.info(f'{opt}---------参数校验通过---------')
                    else:
                        self.log.error(f'{opt}---------参数校验不通过---------')
            else:
                self.log.error('---------参数校验有误---------')

        self.log.info('------------------显示版本号信息------------------')
        cmd_list = ['-V', '--version']
        for cmd in cmd_list:
            check_cmd1 = f'''source {macro.DB_ENV_PATH}
                                gs_collector {cmd}'''
            self.log.info(check_cmd1)
            msg1 = self.primary_node.sh(check_cmd1).result()
            self.log.info(msg1)
            self.assertIn('openGauss', msg1)

        self.log.info('---收集日志信息时指定开始和结束时间---')
        start_time = time.strftime("%Y%m%d %H:%M", time.localtime())
        self.log.info(start_time)
        time.sleep(30)
        end_time = time.strftime("%Y%m%d %H:%M", time.localtime())
        self.log.info(end_time)
        check_cmd1 = f'source {macro.DB_ENV_PATH};' \
            f'gs_collector --begin-time="{start_time}" ' \
            f' --end-time="{end_time}";'
        self.log.info(check_cmd1)
        msg1 = self.primary_node.sh(check_cmd1).result()
        self.log.info(msg1)
        self.assertIn(self.Constant.GS_COLLECTOR_SUCCESS_MSG, msg1)
        time.sleep(5)
        self.log.info('----查看目录下文件--------')
        ls_cmd = f"ls -al {self.tmp_dir_path}"
        self.log.info(ls_cmd)
        ls_msg = self.primary_node.sh(ls_cmd).result()
        self.log.info(ls_msg)

    def tearDown(self):
        self.log.info('--------------无需清理环境-------------------')
        self.log.info(f'-----{os.path.basename(__file__)} end------')
